package com.coolwen.controller;


import com.coolwen.entity.SccocerException;
import com.coolwen.entity.User;
import com.coolwen.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


/**
 * Created by Administrator on 2016/12/5.
 */
@Controller
@RequestMapping(value ={"/user"})
public class UserController {
    //引入log4j,进行日志打印
    private static final Logger LOGGER = Logger.getLogger(UserController.class);
    //登录用户判断用户是否
    @Autowired
    UserService userService;

    //链接到add页面时是GET请求，会访问这段代码，注册表单时先判断用户
    @RequestMapping(value="/add",method=RequestMethod.GET)
    public String add(Model model) {
        //开启modelDriven
        model.addAttribute(new User());

        return "user/register";
    }

    //进入用户注册界面判断用户名是否已经存在，使用user.getUserName方法
    //在具体添加用户时，是post请求，就访问以下代码
    @RequestMapping(value="/add",method=RequestMethod.POST)
    public String add(@Validated User user)  {
        User u = userService.findUserByUserName(user.getUserName());

        if (u!=null) {
            throw new SccocerException("你的输入的用户名"+user.getUserName()+"已存在！");
        }
        userService.addUser(user);
        return "redirect:/team/team/addTeam";
    }

}


